//
//  RootViewController.m
//  Tables
//
//  Created by mathias Muller on 8/20/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "RootViewController.h"
#import "TablesAppDelegate.h"
#import "PreferencesManager.h"

#define kTransitionDuration	0.75

@implementation RootViewController
@synthesize flipButton, doneButton, containerView, settingsView, homeView;

- (void)awakeFromNib {
	self.settingsView = nil;
	[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
	
	// create the container view which we will use for flip animation (centered horizontally)
	[self.view addSubview:containerView];
	
	[containerView addSubview: self.homeView];
	
	// add our custom flip button as the nav bar's custom right view
	UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
	[infoButton addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
	flipButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
	self.navigationItem.rightBarButtonItem = flipButton;
	// create our done button as the nav bar's custom right view for the flipped view (used later)
	doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
															   target:self action:@selector(flipAction:)];
}
- (void)flipAction:(id)sender {
	if(self.settingsView == nil) {
		self.settingsView = [[PreferencesManager  defaultInstance] loadPreferenceNIB];
	}
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(animationDidStop:animationIDfinished:finished:context:)];
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:kTransitionDuration];
	
	[UIView setAnimationTransition:([self.view superview] ?
									UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
						   forView:containerView cache:YES];
	if ([settingsView superview]) {
		[settingsView removeFromSuperview];
		[containerView addSubview:homeView];
	} else {
		[homeView removeFromSuperview];
		[containerView addSubview:settingsView];
	}
	
	[UIView commitAnimations];
	
	// adjust our done/info buttons accordingly
	if ([settingsView  superview]) {
		self.navigationItem.rightBarButtonItem = doneButton;
	} else {
		self.navigationItem.rightBarButtonItem = flipButton;
	}
}


-(IBAction)showStats:(id)sender {
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[super dealloc];
}


@end

